//
//  ZYRegisterViewController.swift
//  WeiBoAPP
//
//  Created by ios001 on 2022/3/8.
//  Copyright © 2022 zxf. All rights reserved.
//

import UIKit


class ZYRegisterViewController: ZFBaseViewController ,UITextFieldDelegate{

    
    var phoneTF:UITextField?
    var pwdTF:UITextField?
    var surePwdTF:UITextField?
    var registerBlock:kLoginSuccessBlock?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    deinit {
        print("对象被delloc没有造成循环引")
    }
}
// MARK: UI
extension ZYRegisterViewController{
    
    override func setupUI() {
        super.setupUI()
        let textFieldView = setupTextFeildUI(topView: nil);
        setupLoginButtonUI(topView: textFieldView)
    }
    override func setupTableView() {
        
    }
    
    // 点击注册
    @objc func registAction() {
        
        self.navigationController?.pushViewController(ZYRegisterViewController.init(), animated: true)
    }
    
    
    // 创建UI
    func setupTextFeildUI(topView:UIView?) -> UIView {
        
        var lastBgView:UIView = UIView.init()
        for i  in 0..<3 {
            let bgView = UIView.init()
            bgView.backgroundColor = UIColor.white
//            bgView.layer.borderColor = UIColor.red.cgColor
//            bgView.layer.borderWidth = 1
            view.addSubview(bgView)
            
            bgView.snp.makeConstraints { make in
                make.height.equalTo(50)
                make.left.right.equalToSuperview()
                if i == 0 {
//                    make.top.equalTo(box1.snp.top).offset(20) //top等于box1.top，再向下偏离20
                    make.top.equalTo(kNavigationHeight)
                }else{
//                    make.top.equalTo(0)
                    make.top.equalTo(lastBgView.snp.bottom)
                }
            }
        
            lastBgView = bgView;
            
            // 标题
            let label = UILabel.init()
            label.font = UIFont.systemFont(ofSize: 15)
            label.textColor = UIColor.black
            label.text = "标题"
            bgView.addSubview(label)
            // 输入框
            let textField = UITextField.init()
            textField.placeholder = "请输入...";
            textField.font  = UIFont.systemFont(ofSize: 14)
            textField.borderStyle = UITextField.BorderStyle.roundedRect
            textField.delegate = self
            bgView.addSubview(textField)
            textField.text = "\(i)"
            if i == 0 {
                phoneTF = textField
                label.text = "用户名"
                textField.placeholder = "请输入用户名";
                textField.returnKeyType = UIReturnKeyType.done
            }else if (i == 1) {
                pwdTF = textField
                label.text = "密码"
                textField.placeholder = "请输入密码";
                textField.keyboardType = UIKeyboardType.numberPad
            }else {
                surePwdTF = textField
                label.text = "确认密码"
                textField.placeholder = "请重复输入密码";
                textField.keyboardType = UIKeyboardType.numberPad
            }

            
            label.snp.makeConstraints { make in
                make.left.equalTo(15)
                make.centerY.equalToSuperview()
                make.width.equalTo(90)
                make.height.equalTo(20)
            }
            
            textField.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.height.equalTo(35)
                make.left.equalTo(label.snp.right).offset(10)
                make.right.equalTo(bgView.snp.right).offset(-15);
            }
            
        }
   
        // 返回最下一个view
        return lastBgView
    }

    
    // 创建UI
    func setupLoginButtonUI(topView:UIView?)  {
        let bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        view.addSubview(bgView)
//        bgView.layer.borderColor = UIColor.red.cgColor
//        bgView.layer.borderWidth = 1
        
        bgView.snp.makeConstraints { make in
            make.left.right.centerY.equalToSuperview()
            make.height.equalTo(50)
        }
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        button.setTitle("注册", for: UIControl.State.normal)
        button.setTitleColor(UIColor.blue, for: UIControl.State.normal)
        button.layer.borderWidth = 0.5
        button.layer.borderColor = UIColor.blue.cgColor
        button.layer.cornerRadius = 5
        button.addTarget(self, action: #selector(loginButtonAction), for: UIControl.Event.touchUpInside)
        bgView.addSubview(button)
        button.snp.makeConstraints { make in
            make.width.equalTo(180)
            make.height.equalTo(40)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        

        
    }
    
    // 登录点击事件
    @objc func loginButtonAction (){
        
        view.endEditing(true)
        print("注册")
        if phoneTF?.text?.count ?? 0 <= 0 {
            showAlert(titleStr: "温馨提示", message: "请输入用户名")
            return
        }
        if pwdTF?.text?.count ?? 0 <= 0 {
            showAlert(titleStr: "温馨提示", message: "请输入密码")
            return
        }
        if surePwdTF?.text?.count ?? 0 <= 0 {
            showAlert(titleStr: "温馨提示", message: "请输入密码")
            return
        }
        
     
        //延迟5秒执行
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
//            print("时间2：", Date())
//        }
        // 延迟5s
        showHuWaiting(title: nil, msg: "正在注册....")
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2.0) {
            self.showHuDing(title: nil, msg: "注册成功")
            print(Thread.isMainThread)
            self.navigationController?.popViewController(animated: true)
            
            // 注册成功回调
            self.registerBlock?()
            
        }
    }
    
        
        
        
                                  
                                  
    
//    let time: NSTimeInterval = 2.0
//    let delay = dispatch_time(DISPATCH_TIME_NOW,
//                             Int64(time * Double(NSEC_PER_SEC)))
//    dispatch_after(delay, dispatch_get_main_queue()) {
//        print("2 秒后输出")
//    }


    
//    // 创建目标队列
//    let workingQueue = dispatch_queue_create("my_queue", nil)
//
//    // 派发到刚创建的队列中，GCD 会负责进行线程调度
//    dispatch_async(workingQueue) {
//        // 在 workingQueue 中异步进行
//        print("努力工作")
//        NSThread.sleepForTimeInterval(2)  // 模拟两秒的执行时间
//
//        dispatch_async(dispatch_get_main_queue()) {
//            // 返回到主线程更新 UI
//            print("结束工作，更新 UI")
//        }
//    }
//

    
}


// MARK: delegate
extension ZYRegisterViewController {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        
        print(textField.text as Any)
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let limitation = 19
        
        let currentLength = textField.text?.count ?? 0 // 当前长度
        if (range.length + range.location > currentLength){
            return false
        }
        // 禁用启用按钮
        let newLength = currentLength + string.count - range.length // 加上输入的字符之后的长度
        if newLength >= limitation {
            //                    confirmButton.isEnable = true
        } else {
            //                    confirmButton.isEnable = false
        }
        return newLength <= limitation
    }



    
}
